package com.example.digitaldiary;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataHelper extends SQLiteOpenHelper {

	private static final String DATABASE_NAME = "Diary.db";
	private static final int SCHEMA_VERSION = 1;

	public DataHelper(Context context) {
		super(context, DATABASE_NAME, null, SCHEMA_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE diarynotes (_id INTEGER PRIMARY KEY AUTOINCREMENT, Note TEXT, DateCreated TEXT, Smiley TEXT, Image TEXT, Lat REAL, Lon REAL, Weather TEXT, Tag TEXT, Category TEXT, GPSNote TEXT);");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE diary_note");

	}

	public Cursor getAll() {
		return (getReadableDatabase()
				.rawQuery("SELECT * FROM diarynotes;", null));
	}

	public void insert(String note, String dateCreated, String image,
			String smiley, Double lat, Double lon, String weather, String tag,
			String category, String GPSnote) {
		ContentValues cv = new ContentValues();
		cv.put("Note", note);
		cv.put("DateCreated", dateCreated);
		cv.put("Smiley", smiley);
		cv.put("Image", image);
		cv.put("Lat", lat);
		cv.put("Lon", lon);
		cv.put("Weather", weather);
		cv.put("Tag", tag);
		cv.put("Category", category);
		cv.put("GPSNote", GPSnote);
		getWritableDatabase().insert("diarynotes", "Note", cv);
	}

	public Cursor getById(String id) {
		String[] args = { id };
		return getReadableDatabase().rawQuery(
				"SELECT * FROM diarynotes WHERE _id = ?", args);
	}

	public String getNote(Cursor c) {
		return (c.getString(1));
	}

	public String getDateCreated(Cursor c) {
		return (c.getString(2));
	}

	public String getSmiley(Cursor c) {
		return c.getString(3);
	}
	public String getImage(Cursor c)
	{
		return(c.getString(4));
	}
	public Double getLat(Cursor c)
	{
		return(c.getDouble(5));
	}
	public Double getLon(Cursor c)
	{
		return(c.getDouble(6));
		
	}
	public String getWeather(Cursor c)
	{
		return(c.getString(7));
	}
	public String getTag(Cursor c)
	{
		return(c.getString(8));
	}
	public String getCategory(Cursor c)
	{
		return(c.getString(9));
	}
	public String getGPSNote(Cursor c)
	{
		return(c.getString(10));
	}
	public void update(String id, String notes, String dateCreated, String images,
			String smiley_id, double longtitude, double latitude,
			String weather, String tags, String categories, String gpsNote) {
		ContentValues cv = new ContentValues();
		cv.put("Note", notes);
		cv.put("DateCreated", dateCreated);
		cv.put("Smiley", smiley_id);
		cv.put("Image", images);
		cv.put("Lat", latitude);
		cv.put("Lon", longtitude);
		cv.put("Weather", weather);
		cv.put("Tag", tags);
		cv.put("Category", categories);
		cv.put("GPSNote", gpsNote);
		String args[] = { id };
		getWritableDatabase().update("diarynotes", cv, "_id=?", args);
		
	}
	
	public void deleteById(String id)
	{
		getWritableDatabase().delete("diarynotes", "_id=?", new String[] { id});
	}

}
